package com.hemerasolutions.v2trainer.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;

import org.primefaces.event.FileUploadEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.hemerasolutions.v2trainer.component.interfaces.table.EquipmentTableCompInterface;
import com.hemerasolutions.v2trainer.controller.interfaces.AcademyBeanInterface;
import com.hemerasolutions.v2trainer.model.multimedia.Photo;
import com.hemerasolutions.v2trainer.model.users.Academy;
import com.hemerasolutions.v2trainer.model.users.User;
import com.hemerasolutions.v2trainer.service.ExceptionService;
import com.hemerasolutions.v2trainer.service.interfaces.LibraryServiceInterface;
import com.hemerasolutions.v2trainer.service.interfaces.UserServiceInterface;


@Controller
@ManagedBean(name="academyBean")
@Scope("view")
public class AcademyBean extends BaseBean implements AcademyBeanInterface{


	private static final long serialVersionUID = 1L;

	@Autowired
	private UserServiceInterface userService;
	
	@Autowired
	private LibraryServiceInterface libraryService;
	
	@Autowired
	private EquipmentTableCompInterface equipmentTable;
	
	private List<Academy> academys;
	
	private Academy selAcademy;
	
	private Academy academy;
	

	private int uploadType;
	
	private Date 		begCreatedAt;
    
	private Date 		endCreatedAt;
    
	private Date 		begChangedAt;
    
	private Date 		endChangedAt;

	private Date 		begExpireOn;
    
	private Date 		endExpireOn;

	private String 		findByName;
    
	private String 		findByCnpj;
    
	private String 		findByEmail;
    
	private String 		findByUserName;
    
	private String 		findByOwner;

	private boolean 	findByStatus = true;	
	
	
	public AcademyBean() {
		super();
	}

	@PostConstruct
	public void init(){
		super.init();
		this.findByStatus = true;
		if(this.getActiveUserType() == User.USERTYPE_ACADEMY){
			this.academy = this.getActiveActor(Academy.class);
			this.equipmentTable.setEquipments(this.academy.getEquipments());	
			this.equipmentTable.refresh();

		}else{
			this.listAll();
		}
	
	}
	

	private void listAll() {

	   	Map<String,Object> fields = new HashMap<String, Object>();
	    this.academys = this.userService.searchAcademys(fields);

	}

	public void clear(){
		this.academy = null;
	}
	
	public void createAcademy(){
		try {
			userService.createAcademy(academy);
			this.showInfoMessage(	this.localizedText("CreatedWithSuccess"),
					this.localizedText("TheAcademy") + " " +
					this.academy.getName() + "\n , " + 
					this.localizedText("wasCreatedWithSuccess"));			
			this.academy = null;
			this.listAll();

		} catch (ExceptionService e) {
			this.showErrorMessage(this.localizedText("CreateError"), e.getLocalizedMessage());
		}
	}
	
	public void saveAcademy(){
		try {
			userService.saveAcademy(academy);
			this.showInfoMessage(	this.localizedText("SavedWithSuccess"),
					this.localizedText("TheAcademy") + " " +
					this.academy.getName() + "\n , " + 
					this.localizedText("wasSavedWithSuccess"));			
			this.listAll();
			
		} catch (ExceptionService e) {
			this.showErrorMessage(this.localizedText("SaveError"), e.getLocalizedMessage());
		}
	}
	
	public void cancelChange(){
		if(this.academy.getId() != 0){
			this.academy = userService.readAcademy(academy);
		}else{
			this.academy = new Academy();
		}
	}

	public void uploadPhoto(FileUploadEvent ev){
		
		if(this.uploadType == 1){		

			Photo photo = new Photo();

			photo.setName("default_logo");
			photo.setHeight(120);
			photo.setWidth(100);
			photo.setLocation(userService.persistPhotoInFile(super.uploadPhoto(ev, photo)));			

			academy.setLogo(photo);

			
		}else if (this.uploadType == 2){

			Photo photo = new Photo();

			photo.setName("default_wide_logo");
			photo.setHeight(80);
			photo.setWidth(250);
			photo.setLocation(userService.persistPhotoInFile(super.uploadPhoto(ev, photo)));			

			academy.setWideLogo(photo);
		}
	}
	

	
	public void search( ){

    	Map<String,Object> fields = new HashMap<String, Object>();
    	
		if( this.begCreatedAt != null && this.endCreatedAt != null && 
				this.begCreatedAt.compareTo(this.endCreatedAt) > 0){
				this.showErrorMessage(	this.localizedText("CreationDate"),
										this.localizedText("TheInitialDateNeedToBeGreaterThanFinal"));
				return;
			}
			

		if( this.begChangedAt != null && this.endChangedAt != null && 
			this.begChangedAt.compareTo(this.endChangedAt) > 0){
			this.showErrorMessage(	this.localizedText("ChangeDate"),
									this.localizedText("TheInitialDateNeedToBeGreaterThanFinal"));
			return;			
		}
    	
		if( this.begExpireOn != null && this.endExpireOn != null && 
				this.begExpireOn.compareTo(this.endExpireOn) > 0){
				this.showErrorMessage(	this.localizedText("ExpireDate"),
										this.localizedText("TheInitialDateNeedToBeGreaterThanFinal"));
				return;
			}
					
    	

    	if (!this.findByName.equals("")) {
            fields.put("name", this.findByName);
        }
    	
    	if (!this.findByOwner.equals("")) {
            fields.put("owner", this.findByOwner);
        }
    	
    	if (!this.findByEmail.equals("")){
    		fields.put("email",this.findByEmail);
    	}
    	
    	if (!this.findByCnpj.equals("")){
    		fields.put("cnpj",this.findByCnpj);
    	}
    	
    	if (this.begCreatedAt != null){
    		fields.put("begCreatedAt", this.begCreatedAt);
    	}
    	if (this.endCreatedAt != null){
            fields.put("endCreatedAt",this.endCreatedAt);
        }
        if (this.begChangedAt != null){
        	fields.put("begChangedAt",this.begChangedAt);
        }
        if (this.endChangedAt != null){
        	fields.put("endChangedAt", this.endChangedAt);
        }

        if (this.begExpireOn != null){
        	fields.put("begExpireOn",this.begExpireOn);
        }
        if (this.endExpireOn != null){
        	fields.put("endExpireOn", this.endExpireOn);
        }

   	    fields.put("active", this.findByStatus);

		
	    this.academys = this.userService.searchAcademys(fields);

		if((this.academys).isEmpty()){
			this.showInfoMessage(	this.localizedText("SearchCompleted"),
									this.localizedText("NoEntrysFounded"));
		}else{
			this.showInfoMessage(	this.localizedText("SearchCompleted"),
									this.academys.size() + " " + 
									this.localizedText("entrysFounded"));
		}
	}
	

	
	public void deleteAcademy(){
		
		try{
			userService.deleteAcademy(academy);
			this.showInfoMessage(	this.localizedText("Deleted"),
									this.localizedText("UserDeletedWithSuccess"));
			this.academys.remove(this.academy);
			this.academy = null;
		}catch(ExceptionService e){
			this.showErrorMessage(this.localizedText("DeleteError"),
					e.getLocalizedMessage());
		}
		
	}
	
	
	public void toAddAcademy(){
		this.academy  = new Academy();
		
		this.academy.setLogo(userService.copyDefaultLogo());
		this.academy.setWideLogo(userService.copyDefaultWideLogo());
		this.academy.getUser().setLibrarys(libraryService.copyPublicLibrarys());
		this.equipmentTable.setEquipments(this.academy.getEquipments());	
		this.equipmentTable.refresh();
		
	}

	public void toViewAcademy(){
		this.equipmentTable.setEquipments(this.academy.getEquipments());	
		this.equipmentTable.refresh();
	}
	
	public List<Academy> getAcademys() {
		return academys;
	}

	public Academy getSelAcademy() {
		return selAcademy;
	}

	public Academy getAcademy() {
		return academy;
	}

	public void setAcademys(List<Academy> academys) {
		this.academys = academys;
	}

	public void setSelAcademy(Academy selAcademy) {
		this.selAcademy = selAcademy;
	}

	public void setAcademy(Academy academy) {
		this.academy = academy;
	}

	public int getUploadType() {
		return uploadType;
	}

	public void setUploadType(int uploadType) {
		this.uploadType = uploadType;
	}


	public Date getBegCreatedAt() {
		return begCreatedAt;
	}

	public Date getEndCreatedAt() {
		return endCreatedAt;
	}

	public Date getBegChangedAt() {
		return begChangedAt;
	}

	public Date getEndChangedAt() {
		return endChangedAt;
	}

	public String getFindByName() {
		return findByName;
	}

	public String getFindByCnpj() {
		return findByCnpj;
	}

	public String getFindByEmail() {
		return findByEmail;
	}

	public String getFindByUserName() {
		return findByUserName;
	}


	public boolean isFindByStatus() {
		return findByStatus;
	}

	public void setBegCreatedAt(Date begCreatedAt) {
		this.begCreatedAt = begCreatedAt;
	}

	public void setEndCreatedAt(Date endCreatedAt) {
		this.endCreatedAt = endCreatedAt;
	}

	public void setBegChangedAt(Date begChangedAt) {
		this.begChangedAt = begChangedAt;
	}

	public void setEndChangedAt(Date endChangedAt) {
		this.endChangedAt = endChangedAt;
	}

	public void setFindByName(String findByName) {
		this.findByName = findByName;
	}

	public void setFindByCnpj(String findByCnpj) {
		this.findByCnpj = findByCnpj;
	}

	public void setFindByEmail(String findByEmail) {
		this.findByEmail = findByEmail;
	}

	public void setFindByUserName(String findByUserName) {
		this.findByUserName = findByUserName;
	}

	public void setFindByStatus(boolean findByStatus) {
		this.findByStatus = findByStatus;
	}


	public String getFindByOwner() {
		return findByOwner;
	}

	public void setFindByOwner(String findByOwner) {
		this.findByOwner = findByOwner;
	}

	public Date getBegExpireOn() {
		return begExpireOn;
	}

	public Date getEndExpireOn() {
		return endExpireOn;
	}

	public void setBegExpireOn(Date begExpireOn) {
		this.begExpireOn = begExpireOn;
	}

	public void setEndExpireOn(Date endExpireOn) {
		this.endExpireOn = endExpireOn;
	}

	public EquipmentTableCompInterface getEquipmentTable() {
		return equipmentTable;
	}

	public void setEquipmentTable(EquipmentTableCompInterface equipmentTable) {
		this.equipmentTable = equipmentTable;
	}
	
	
	
}
